package com.dankegongyu.lib.http.observer;

import com.dankegongyu.lib.http.exception.ApiException;

import io.reactivex.annotations.NonNull;
import io.reactivex.observers.DisposableObserver;

/**
 * @author wpq
 * @version 1.0
 */
public abstract class DKHttpBaseObserver<T> extends DisposableObserver<T> {

    protected abstract void onSuccess(@NonNull T data);

    protected abstract void onFailure(@NonNull ApiException e);

    @Override
    public void onNext(@NonNull T t) {
        onSuccess(t);
    }

    @Override
    public void onError(Throwable e) {
        onFinish();
        if (e instanceof ApiException) {
            onFailure((ApiException) e);
        } else {
            onFailure(new ApiException(e));
        }
    }

    @Override
    public void onComplete() {
        onFinish();
    }

    protected void onFinish() {

    }
}
